package com.cn.designpatterns.ex.concrete.observer.objdkimpl;


import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

/**
 * @author zhangyi
 * @date 2018/12/25 9:50
 */
public class ObserverableJdk extends Observable {

    private List<Observer> list;

    private Integer price;
    public ObserverableJdk(Integer price) {
        this.price = price;
        list = new Vector<>();
        notifyObservers(price);
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    @Override
    public synchronized void addObserver(Observer o) {
        list.add(o);
    }

    @Override
    public synchronized void deleteObserver(Observer o) {
        list.remove(o);
    }

    @Override
    public void notifyObservers() {
        for(Observer o : list){
            o.update(this,price);
        }
    }
}
